package com.xingchi.tornado.storage.core.config;

import com.xingchi.tornado.storage.core.enums.PlatformTypeEnum;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.time.Duration;

/**
 * 存储配置类
 *
 * @author xingchi
 * @date 2024/5/3 19:56
 */
@Data
@Slf4j
@ConfigurationProperties(prefix = StorageProperties.BASE_CONFIG_PREFIX)
public class StorageProperties implements InitializingBean {

    public static final String BASE_CONFIG_PREFIX = "xingchi.tornado.file.storage";
    public static final String STORAGE_CONFIG_PREFIX = "xingchi.tornado.file.storage.type";

    /**
     * 存储类型
     */
    private PlatformTypeEnum type = PlatformTypeEnum.LOCAL;

    /**
     * minio相关配置
     */
    private MinioProperties minio;

    /**
     * oss相关配置
     */
    private OSSProperties oss;

    /**
     * 本地存储相关配置
     */
    private LocalProperties local;

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("file storage service config init finish! use platform is '{}'", this.type);
    }


    @Data
    public static class MinioProperties {

        /**
         * 域名
         */
        private String domain;

        /**
         * 访问地址
         */
        private String endpoint = "http://localhost:9000";

        /**
         * 访问key
         */
        private String accessKey;

        /**
         * 访问密钥
         */
        private String secretKey;

        /**
         * 存储桶
         */
        private String bucketName = "default";

        /**
         * 是否加密访问，默认为true
         */
        private boolean encrypt = true;

        /**
         * 访问过期时间，默认60分钟
         */
        private Duration expireTime = Duration.ofMinutes(60);

        /**
         * 是否自动创建bucket
         */
        private boolean auto = false;

    }


    @Data
    public static class OSSProperties {

        /**
         * 域名
         */
        private String domain;

        /**
         * 访问地址
         */
        private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";

        /**
         * 访问key
         */
        private String accessKey;

        /**
         * 访问密钥
         */
        private String secretKey;

        /**
         * 存储桶
         */
        private String bucketName = "default";

        /**
         * 访问过期时间，默认60分钟
         */
        private Duration expireTime = Duration.ofMinutes(60);

        /**
         * 是否自动创建bucket
         */
        private boolean auto = false;

    }

    @Data
    public static class LocalProperties {

        /**
         * 文件存储路径
         */
        String filePath;

        /**
         * 路由前缀
         */
        String routePrefix;

        /**
         * 域名
         */
        String domain;

    }
}
